﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utils;
using Bll;
using Analysis.Models.EchartModel;

namespace Analysis.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult TestPull()
        {
            return View();
        }

        public ActionResult TestScroll()
        {
            return View();
        }

        public ActionResult Index()
        {
            //if (Session["User"] == null)
            //{
            //    return RedirectToAction("Index", "LoginManager");
            //}
            DateTime start = DateTime.Now;
            LogHelper.WriteInfoLog("首页进入时间=" + start.ToString("yyyy-MM-dd HH:mm:ss"));
            //今日订单数，今日总访客，今日总流量  今日总销售额，今日实体销售额，今日电商销售额  今日浏览商品，今日客单价
             List<object> list= GetTodayData();

            //今日总订单数
             int today_all_order_num = list[0].ConToInt();
            //今日实体订单数
             int today_syj_order_num = list[8].ConToInt(); ;
            //今日电商订单数
             int today_bbc_order_num = list[9].ConToInt(); ;
            //今日总访客
             int today_online_login_num = list[1].ConToInt();
            //今日总流量(只算进详情页)
             int today_online_flow_num = list[2].ConToInt();

             //今日总销售额
             decimal today_all_sale_amount = list[3].ConToDecimal();
            //今日实体店销售额
             decimal today_shop_sale_amount = list[4].ConToDecimal();
            //今日线上销售额
            decimal today_online_sale_amount = list[5].ConToDecimal();


            //今日线上浏览商品数
            int today_online_scan_product = list[6].ConToInt(); ;
            //今日客单价
            decimal today_custom_per_order = list[7].ConToDecimal();
           

            #region 昨日客单价

            //昨日实体店订单数
            int yesterday_shop_order_num = 0;
            string sql_yesterday_shop_order_num = "select count(1) as num from t_pos_syd where ct<convert(varchar(30),getdate(),111) and ct>dateadd(d,-1,getdate()) and yss#=0 and s# not in(select yss# from t_pos_syd where yss#>0 and ct<convert(varchar(30),getdate(),111) and ct>dateadd(d,-1,getdate()))";
            yesterday_shop_order_num = BllSYJCommon.GetCountBySQL("t_pos_syd", sql_yesterday_shop_order_num);
            //昨日线上订单数
            int yesterday_online_order_num = 0;
            string sql_yesterday_online_order_num = "select count(1) as num from orders where paystatusId=2 and payTime<convert(varchar(30),getdate(),111) and payTime>dateadd(d,-1,getdate()) and plateform=3";
            yesterday_online_order_num = BllBBCCommon.GetCountBySQL("orders", sql_yesterday_online_order_num);
            //昨日总订单数
            int yesterday_all_order_num = yesterday_shop_order_num + yesterday_online_order_num;

            //昨日实体店销售额
            decimal yesterday_shop_sale_amount = 0;
            string sql_yesterday_shop_sale_amount = "select isnull(sum(dcpayment-dcchange-isnull(mlje,0)),0) as amount from t_pos_syd where ct<convert(varchar(30),getdate(),111) and ct>dateadd(d,-1,getdate())";
            yesterday_shop_sale_amount = BllSYJCommon.SelectSingleValueBySql("t_pos_syd", sql_yesterday_shop_sale_amount, "decimal").ConToDecimal();
            //昨日线上销售额
            decimal yesterday_online_sale_amount = 0;
            string sql_yesterday_online_sale_amount = "select isnull(sum(realTotal),0) as amount from orders where paystatusId=2 and payTime<convert(varchar(30),getdate(),111) and payTime>dateadd(d,-1,getdate()) and plateform=3";
            yesterday_shop_sale_amount = BllBBCCommon.SelectSingleValueBySql("orders", sql_yesterday_online_sale_amount, "decimal").ConToDecimal();
            //昨日总销售额
            decimal yesterday_all_sale_amount = 0;
            yesterday_all_sale_amount = yesterday_shop_sale_amount + yesterday_online_sale_amount;

            //昨日客单价
            decimal yesterday_custom_per_order = 0;
            if (yesterday_all_order_num > 0)
            {
                yesterday_custom_per_order = Math.Round(yesterday_all_sale_amount / yesterday_all_order_num, 2);
            }
            #endregion

            ViewBag.today_all_order_num = today_all_order_num;
            ViewBag.today_syj_order_num = today_syj_order_num;
            ViewBag.today_bbc_order_num = today_bbc_order_num;

            ViewBag.today_online_login_num = today_online_login_num;
            ViewBag.today_online_flow_num = today_online_flow_num;

            ViewBag.today_all_sale_amount = today_all_sale_amount;
            ViewBag.today_shop_sale_amount = today_shop_sale_amount;
            ViewBag.today_online_sale_amount = today_online_sale_amount;

            ViewBag.today_online_scan_product = today_online_scan_product;
            ViewBag.today_custom_per_order = today_custom_per_order;
            ViewBag.yesterday_custom_per_order = yesterday_custom_per_order;

            return View();
        }

        /// <summary>
        /// 获取今日数据
        /// </summary>
        /// <returns>今日订单数，今日总访客，今日总流量
        ///         今日总销售额，今日实体销售额，今日电商销售额  
        ///         今日浏览商品，今日客单价
        ///         实体订单数，电商订单数</returns>
        public List<object> GetTodayData()
        {
            List<object> list = new List<object>();

            //今日实体店订单数
            int today_shop_order_num = 0;
            string sql_today_shop_order_num = "select count(1) as num from t_pos_syd where ct>convert(varchar(30),getdate(),111) and yss#=0 and s# not in(select yss# from t_pos_syd where yss#>0 and ct>convert(varchar(30),getdate(),111))";
            today_shop_order_num = BllSYJCommon.GetCountBySQL("t_pos_syd", sql_today_shop_order_num);
            //今日线上订单数
            int today_online_order_num = 0;
            string sql_today_online_order_num = "select count(1) as num from orders where paystatusId=2 and payTime>convert(varchar(30),getdate(),111) and plateform=3";
            today_online_order_num = BllBBCCommon.GetCountBySQL("orders", sql_today_online_order_num);
            //今日总订单数
            int today_all_order_num = today_shop_order_num + today_online_order_num;

            //今日总访客
            int today_online_login_num = 0;
            string sql_today_online_login_num = "select count(distinct(userName)) as total from UserLoginRecord where createTime>convert(varchar(30),getdate(),111)";
            today_online_login_num = BllBBCCommon.GetCountBySQL("UserLoginRecord", sql_today_online_login_num);
            //今日总流量(只算进详情页)
            int today_online_flow_num = 0;
            string sql_today_online_flow_num = "select count(1) as num from BrowseRecord where createTime>convert(varchar(30),getdate(),111)";
            today_online_flow_num = BllBBCCommon.GetCountBySQL("BrowseRecord", sql_today_online_flow_num);


            //今日实体店销售额
            decimal today_shop_sale_amount = 0;
            string sql_today_shop_sale_amount = "select isnull(sum(dcpayment-dcchange-isnull(mlje,0)),0) as amount from t_pos_syd where ct>convert(varchar(30),getdate(),111)";
            today_shop_sale_amount = BllSYJCommon.SelectSingleValueBySql("t_pos_syd", sql_today_shop_sale_amount, "decimal").ConToDecimal();
            //今日线上销售额
            decimal today_online_sale_amount = 0;
            string sql_today_online_sale_amount = "select isnull(sum(realTotal),0) as amount from orders where paystatusId=2 and payTime>convert(varchar(30),getdate(),111) and plateform=3";
            today_online_sale_amount = BllBBCCommon.SelectSingleValueBySql("orders", sql_today_online_sale_amount, "decimal").ConToDecimal();
            //今日总销售额
            decimal today_all_sale_amount = 0;
            today_all_sale_amount = today_shop_sale_amount + today_online_sale_amount;

            //今日线上浏览商品数
            int today_online_scan_product = 0;
            string sql_today_online_scan_product = "select count(distinct(productId))  from BrowseRecord where createTime>convert(varchar(30),getdate(),111)";
            today_online_scan_product = BllBBCCommon.GetCountBySQL("BrowseRecord", sql_today_online_scan_product);
            //今日客单价
            decimal today_custom_per_order = 0;
            if (today_all_order_num > 0)
            {
                today_custom_per_order = Math.Round(today_all_sale_amount / today_all_order_num, 2);
            }

            list.Add(today_all_order_num);
            list.Add(today_online_login_num);
            list.Add(today_online_flow_num);

            list.Add(today_all_sale_amount);
            list.Add(today_shop_sale_amount);
            list.Add(today_online_sale_amount);

            list.Add(today_online_scan_product);
            list.Add(today_custom_per_order);

            list.Add(today_shop_order_num);
            list.Add(today_online_order_num);

            return list;
        }

        /// <summary>
        /// 刷新今日数据
        /// </summary>
        /// <returns></returns>
        public JsonResult FreshToday()
        {
             List<object> list= GetTodayData();

             return Json(list,JsonRequestBehavior.AllowGet);
        }

        public JsonResult ProductIncomeTendency(string beginTime, string endTime,string plateformType)
        {
            Result<BarOrLineModel> result = new Result<BarOrLineModel>();

            beginTime = Extend.ConToBeginTime(beginTime);
            endTime = Extend.ConToEndTime(endTime);
            plateformType = string.IsNullOrEmpty(plateformType) ? "-1" : plateformType; //-1：全部 0：实体店 1：电商

            List<string> legendList = new List<string>();
            List<string> nameList = new List<string>();
            List<List<decimal>> dataList = new List<List<decimal>>();

            DateTime now = DateTime.Now;
            DateTime dateTemp = beginTime.ConToDateTime(now);
            DateTime dateTemp2 = endTime.ConToDateTime(now);
            bool isToday = dateTemp.ToShortDateString() == dateTemp2.ToShortDateString();
            string msg = "";
            if (isToday)
            {
                msg = "1";
                #region 当日数据
                List<ProductController.HourDetailModel> detailList = new List<ProductController.HourDetailModel>();

                List<SelectForIncomeTendency> allList = new List<SelectForIncomeTendency>();
                List<SelectForIncomeTendency> allList2 = new List<SelectForIncomeTendency>();
                List<SelectForIncomeTendency> bbcList = new List<SelectForIncomeTendency>();
                List<SelectForIncomeTendency> bbcList2 = new List<SelectForIncomeTendency>();
                List<SelectForIncomeTendency> syjList = new List<SelectForIncomeTendency>();
                List<SelectForIncomeTendency> syjList2 = new List<SelectForIncomeTendency>();

                if (plateformType == "-1")
                {
                    bbcList = BBCHourTendency(beginTime, endTime);
                    allList.AddRange(bbcList);
                    bbcList2 = BBCHourTendency(beginTime, endTime, true);
                    allList2.AddRange(bbcList2);
                    syjList = SYJHourTendency(beginTime, endTime);
                    syjList2 = SYJHourTendency(beginTime, endTime, true);

                    #region 整合
                    foreach (SelectForIncomeTendency incomeSYJ in syjList)
                    {
                        List<string> temp = allList.Select(x => x.timeStr).ToList();

                        if (temp.Contains(incomeSYJ.timeStr))
                        {
                            SelectForIncomeTendency target = allList.Where(x => x.timeStr == incomeSYJ.timeStr).First();
                            target.income += incomeSYJ.income;
                        }
                        else
                        {
                            allList.Add(incomeSYJ);
                        }
                    }

                    foreach (SelectForIncomeTendency incomeSYJ2 in syjList2)
                    {
                        List<string> temp2 = allList2.Select(x => x.timeStr).ToList();

                        if (temp2.Contains(incomeSYJ2.timeStr))
                        {
                            SelectForIncomeTendency target2 = allList2.Where(x => x.timeStr == incomeSYJ2.timeStr).First();
                            target2.income += incomeSYJ2.income;
                        }
                        else
                        {
                            allList2.Add(incomeSYJ2);
                        }
                    }
                    #endregion
                }
                else if (plateformType == "0")
                {
                    syjList = SYJHourTendency(beginTime, endTime);
                    allList.AddRange(syjList);
                    syjList2 = SYJHourTendency(beginTime, endTime, true);
                    allList2.AddRange(syjList2);
                }
                else if (plateformType == "1")
                {
                    bbcList = BBCHourTendency(beginTime, endTime);
                    allList.AddRange(bbcList);
                    bbcList2 = BBCHourTendency(beginTime, endTime, true);
                    allList2.AddRange(bbcList2);
                }


                detailList = CaculateHourRate(allList, allList2);

                legendList.Add("昨日销售额");
                legendList.Add("今日销售额");
                legendList.Add("增长率");

                List<decimal> totalNowList = new List<decimal>();
                List<decimal> totalBeforeList = new List<decimal>();
                List<decimal> rateList = new List<decimal>();

                Random r1 = new Random();
                foreach (ProductController.HourDetailModel obj in detailList)
                {
                    string[] temp = obj.timeStr.Split('-');
                    string time = temp[0] + ":00-" + temp[1] + ":00";
                    nameList.Add(time);
                    totalNowList.Add(obj.incomeNow);
                    totalBeforeList.Add(obj.incomeBefore);
                    rateList.Add(obj.rate);
                }

                dataList.Add(totalBeforeList);
                dataList.Add(totalNowList);
                dataList.Add(rateList);


                #endregion
            }
            else
            {
                msg = "0";
                #region 多日数据

                List<SelectForIncomeTendency> onlineTendency = new List<SelectForIncomeTendency>();
                List<SelectForIncomeTendency> shopTendency = new List<SelectForIncomeTendency>();
                List<SelectForIncomeTendency> allTendency = new List<SelectForIncomeTendency>();

                List<SelectForReturnTendency> onlineReturnTendency = new List<SelectForReturnTendency>();
                List<SelectForReturnTendency> shopReturnTendency = new List<SelectForReturnTendency>();
                List<SelectForReturnTendency> allReturnTendency = new List<SelectForReturnTendency>();

                if (plateformType == "-1")
                {
                    onlineTendency = BBCDayIncomeTendency(beginTime,endTime);
                    shopTendency = SYJDayIncomeTendency(beginTime,endTime);
                    allTendency.AddRange(shopTendency);

                    onlineReturnTendency = BBCDayReturnTendency(beginTime,endTime);
                    shopReturnTendency = SYJDayReturnTendency(beginTime, endTime);
                    allReturnTendency.AddRange(shopReturnTendency);

                   #region 整合
                   foreach (SelectForIncomeTendency tendency in onlineTendency)
                   {
                       bool flag = allTendency.Select(x => x.timeStr).Contains(tendency.timeStr);
                       if (flag)
                       {
                           SelectForIncomeTendency xx = allTendency.Where(x => x.timeStr == tendency.timeStr).First();
                           xx.income += tendency.income;
                       }
                       else
                       {
                           allTendency.Add(tendency);
                       }
                   }

                   foreach (SelectForReturnTendency tendency in onlineReturnTendency)
                   {
                       bool flag = allReturnTendency.Select(x => x.timeStr).Contains(tendency.timeStr);
                       if (flag)
                       {
                           SelectForReturnTendency xx = allReturnTendency.Where(x => x.timeStr == tendency.timeStr).First();
                           xx.amount += tendency.amount;
                       }
                       else
                       {
                           allReturnTendency.Add(tendency);
                       }
                   }

                   #endregion
                }
                else if (plateformType == "0")
                {
                    shopTendency = SYJDayIncomeTendency(beginTime, endTime);
                    allTendency.AddRange(shopTendency);

                    shopReturnTendency = SYJDayReturnTendency(beginTime, endTime);
                    allReturnTendency.AddRange(shopReturnTendency);
                }
                else if (plateformType == "1")
                {
                    onlineTendency = BBCDayIncomeTendency(beginTime, endTime);
                    allTendency.AddRange(onlineTendency);

                    onlineReturnTendency = BBCDayReturnTendency(beginTime, endTime);
                    allReturnTendency.AddRange(onlineReturnTendency);
                }

                List<ProductController.DayDetailModel> dayDetailList = CaculateDayRate(allTendency, allReturnTendency, beginTime, endTime);

                legendList.Add("销售额");
                legendList.Add("增长率");
                legendList.Add("退款单数");


                List<decimal> totalList = new List<decimal>();
                List<decimal> rateList = new List<decimal>();
                List<decimal> returnList = new List<decimal>();


                foreach (ProductController.DayDetailModel obj in dayDetailList)
                {
                    nameList.Add(obj.timeStr);
                    totalList.Add(obj.income);
                    returnList.Add(obj.returnNum);
                    rateList.Add(obj.rate);
                }
                dataList.Add(totalList);
                dataList.Add(rateList);
                dataList.Add(returnList);
                #endregion
            }

            BarOrLineModel bar = new BarOrLineModel();
            bar.NameList = nameList;
            bar.LegendList = legendList;
            bar.DataList = dataList;

            result.Status = 200;
            result.Body = bar;
            result.Msg = msg;

            DateTime end = DateTime.Now;
            LogHelper.WriteInfoLog("首页结束时间=" + end.ToString("yyyy-MM-dd HH:mm:ss"));

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        private class SelectForIncomeTendency
        {
            public string timeStr { get; set; }

            public decimal income { get; set; }
        }

        private class SelectForReturnTendency
        {
            public string timeStr { get; set; }

            public int amount { get; set; }
        }

        private List<SelectForReturnTendency> BBCDayReturnTendency(string beginTime, string endTime)
        {
            string sql_online_day_return = "select convert(char(10), lastModifyTime, 120) as timeStr, count(1) as amount from RefundmentOrder where Status=3 and lastModifyTime>='" + beginTime + "' and lastModifyTime<='" + endTime + "'  group by convert(char(10), lastModifyTime, 120)";
            List<SelectForReturnTendency> onlineTendency = BllBBCCommon.SelectEntityBySql<SelectForReturnTendency>("RefundmentOrder", sql_online_day_return);

            return onlineTendency;
        }

        private List<SelectForReturnTendency> SYJDayReturnTendency(string beginTime, string endTime)
        {
            string sql_shop_day_return = "select convert(char(10), ct, 120) as timeStr, count(1) as amount from t_pos_syd where yss#!=0 and ct>='" + beginTime + "' and ct<='" + endTime + "' group by convert(char(10), ct, 120)";
            List<SelectForReturnTendency> syjTendency = BllSYJCommon.SelectEntityBySql<SelectForReturnTendency>("t_pos_syd", sql_shop_day_return);

            return syjTendency;
        }

        private List<SelectForIncomeTendency> BBCDayIncomeTendency(string beginTime,string endTime)
        {
            string sql_online_day_income = "select convert(char(10), payTime, 120) as timeStr, isnull(sum(realTotal),0) as income from orders where payStatusId=2 and payTime>='" + beginTime + "' and payTime<='" + endTime + "' and plateform=3 group by convert(char(10), payTime, 120)";
           
            List<SelectForIncomeTendency> onlineTendency = BllBBCCommon.SelectEntityBySql<SelectForIncomeTendency>("orders", sql_online_day_income);

            return onlineTendency;
        }

        private List<SelectForIncomeTendency> SYJDayIncomeTendency(string beginTime, string endTime)
        {
            string sql_shop_day_income = "select convert(char(10), ct, 120) as timeStr, isnull(sum(dcpayment-dcchange-isnull(mlje,0)),0) as income from t_pos_syd where ct>='" + beginTime + "' and ct<='" + endTime + "' group by convert(char(10), ct, 120)";
            List<SelectForIncomeTendency> shopTendency = BllSYJCommon.SelectEntityBySql<SelectForIncomeTendency>("t_pos_syd", sql_shop_day_income);

            return shopTendency;
        }

        /// <summary>
        /// 计算同比增长率
        /// </summary>
        /// <param name="incomeNowList">今天收益</param>
        /// <param name="incomeBeforeList">昨日收益</param>
        /// <returns></returns>
        private List<ProductController.HourDetailModel> CaculateHourRate(List<SelectForIncomeTendency> incomeNowList, List<SelectForIncomeTendency> incomeBeforeList)
        {
            #region 转换成24小时制度
            for (int i = 0; i < 24; i++)
            {
                bool flag = incomeNowList.Select(x => x.timeStr).Contains(i.ToString());
                if (!flag)
                {
                    SelectForIncomeTendency income = new SelectForIncomeTendency();
                    income.timeStr = i.ToString();
                    income.income = 0;
                    incomeNowList.Add(income);
                }

            }
            #endregion

            //正向排序
            incomeNowList = incomeNowList.OrderBy(x => x.timeStr.ConToInt()).ToList();

            List<ProductController.HourDetailModel> detailList = new List<ProductController.HourDetailModel>();
            foreach (SelectForIncomeTendency incomeNow in incomeNowList)
            {
                ProductController.HourDetailModel model = new ProductController.HourDetailModel();
                model.timeStr = incomeNow.timeStr + "-" + (incomeNow.timeStr.ConToInt() + 1);
                model.incomeNow = incomeNow.income;
                SelectForIncomeTendency before = incomeBeforeList.Where(x => x.timeStr == incomeNow.timeStr).FirstOrDefault();
                if (before != null)
                {
                    model.incomeBefore = before.income;
                }
                else
                {
                    model.incomeBefore = 0;
                }
                if (model.incomeBefore != 0)
                {
                    model.rate = Math.Round((incomeNow.income - model.incomeBefore) / model.incomeBefore, 4) * 100;
                }
                else
                {
                    model.rate = 0;
                }
                detailList.Add(model);
            }

            return detailList;

        }

        /// <summary>
        /// 计算同比增长率
        /// </summary>
        /// <param name="allList">订单收益趋势</param>
        /// <param name="allReturnTendency">退款单数量趋势</param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<ProductController.DayDetailModel> CaculateDayRate(List<SelectForIncomeTendency> allList, List<SelectForReturnTendency> allReturnTendency, string beginTime, string endTime)
        {
            DateTime beginTime_ = beginTime.ConToDateTime(DateTime.Now);
            DateTime endTime_ = endTime.ConToDateTime(DateTime.Now);
            DateTime start = Convert.ToDateTime(beginTime_.ToShortDateString());
            DateTime end = Convert.ToDateTime(endTime_.ToShortDateString());
            TimeSpan sp = end.Subtract(start);
            int dayNum = sp.Days;

            for (int i = 0; i <= dayNum; i++)
            {
                string temp = beginTime_.AddDays(i).ToString("yyyy-MM-dd");

                bool flag = allList.Select(x => x.timeStr).Contains(temp);
                if (!flag)
                {
                    SelectForIncomeTendency income = new SelectForIncomeTendency();
                    income.timeStr = temp;
                    income.income = 0;

                    allList.Add(income);
                }
            }

            //按日期排序
            allList = allList.OrderBy(x => x.timeStr).ToList();

            List<ProductController.DayDetailModel> detailList = new List<ProductController.DayDetailModel>();
            decimal beforeIncome = 0;
            foreach (SelectForIncomeTendency incomeBBC in allList)
            {
                ProductController.DayDetailModel model = new ProductController.DayDetailModel();
                model.timeStr = incomeBBC.timeStr;
                model.income = incomeBBC.income;
                var temp=allReturnTendency.Where(x => x.timeStr == incomeBBC.timeStr).FirstOrDefault();
                if(temp!=null){
                    model.returnNum = temp.amount;
                }
                if (beforeIncome > 0)
                {
                    model.rate = Math.Round((model.income - beforeIncome) / beforeIncome, 4) * 100;
                }
                else
                {
                    model.rate = 0;
                }

                beforeIncome = incomeBBC.income;
                detailList.Add(model);
            }

            return detailList;

        }


        private List<SelectForIncomeTendency> BBCHourTendency(string beginTime, string endTime, bool isYestoday = false)
        {
            string beginTime_ = beginTime.ConToDateTime(DateTime.Now).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            string endTime_ = endTime.ConToDateTime(DateTime.Now).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            List<SelectForIncomeTendency> tendencyList = new List<SelectForIncomeTendency>();

            string sql_product_online_hour_income = "";
            if (isYestoday)
            {
                sql_product_online_hour_income = "select convert(char(2), o.payTime, 108) as timeStr, isnull(sum(od.totalMoney),0) as income from orders as o,orderDetail as od where o.id=od.orderId  and o.payStatusId=2 and o.payTime<convert(char(10),GetDate(),120) and o.payTime>='"+beginTime_+"' and o.payTime<'"+endTime_+"' and plateform=3 group by convert(char(2), o.payTime, 108)";
            }
            else
            {
                sql_product_online_hour_income = "select convert(char(2), o.payTime, 108) as timeStr, isnull(sum(od.totalMoney),0) as income from orders as o,orderDetail as od where o.id=od.orderId and  o.payStatusId=2 and o.payTime>='" + beginTime + "' and o.payTime<'" + endTime + "' and plateform=3 group by convert(char(2), o.payTime, 108)";
            }
            tendencyList = BllBBCCommon.SelectEntityBySql<SelectForIncomeTendency>("", sql_product_online_hour_income);

            return tendencyList;
        }
        /// <summary>
        /// SYJ日趋势
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private List<SelectForIncomeTendency> SYJHourTendency(string beginTime, string endTime, bool isYestoday = false)
        {
            string beginTime_ = beginTime.ConToDateTime(DateTime.Now).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            string endTime_ = endTime.ConToDateTime(DateTime.Now).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            List<SelectForIncomeTendency> tendencyList = new List<SelectForIncomeTendency>();

            string sql_product_shop_hour_income = "";
            if (isYestoday)
            {
                sql_product_shop_hour_income = "select convert(char(2), sd.ct, 108) as timeStr, isnull(sum(sd.dcpayment-sd.dcchange),0) as income from t_pos_syd as sd,t_pos_sys as ss where sd.s#=ss.d# and sd.ct>='"+beginTime_+"' and sd.ct<'"+endTime_+"' group by convert(char(2), sd.ct, 108)";
            }
            else
            {
                sql_product_shop_hour_income = "select convert(char(2), sd.ct, 108) as timeStr, isnull(sum(sd.dcpayment-sd.dcchange),0) as income from t_pos_syd as sd,t_pos_sys as ss where sd.s#=ss.d# and sd.ct>='"+beginTime+"' and sd.ct<'"+endTime+"' group by convert(char(2), sd.ct, 108)";
            }
            tendencyList = BllSYJCommon.SelectEntityBySql<SelectForIncomeTendency>("", sql_product_shop_hour_income);

            return tendencyList;
        }

    }
}